import { IRequest } from './type'
import { UserController, TeacherController } from './controller'

const fnDebugLog = require('debug')('main')

class Module {
  public controllersList: any[] = [ ]
  constructor (ctrlClassList: any[]) {
    ctrlClassList.forEach((ctrlClass)=> {
      this.controllersList.push(new ctrlClass())
    })
  }
}

const requestsList: IRequest[] = [
  { method: 'get', route: 'users',      option: { params: { 'age'    : '>10' }}},
  { method: 'get', route: 'users/vip',  option: { params: { 'name'   : 'mike'}}},
  { method: 'get', route: 'teachers',   option: { params: { 'subject': 'cs'}}},
]

function initial() {
  return new Module([UserController, TeacherController])
}

function main () {

  const m = initial();
  const controllers = m.controllersList;
  requestsList.forEach((req) => {
    fnDebugLog('+++++++ request: ', req);
    for (let controller of controllers) {
      fnDebugLog('...', controller, controller.root)
      switch(req.method) {
        case 'get':
          {
            controller.handlersOfGet?.forEach((handler:any) => {
              const methodRoute = !handler.route ? controller.root : `${controller.root}/${handler.route}`;
              if (methodRoute !== req.route) return;
              const result = controller[handler.method](req.option)
              fnDebugLog('result', result)
            })
          }
          break;
      }
    }
    fnDebugLog('------ request ------')
  })
}

main()
